package com.bzkj.web.controller.ordermanagement;

import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.document.utils.common.InventoryManagerUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @projectName: prt-wms
 * @package: com.bzkj.web.controller.ordermanagement
 * @className: BasicCommonController
 * @description: 单据处理公共控制层
 * @author: C1309
 * @createDate: 2025年01月13日
 * @version: v1.0
 */
@RestController
@RequestMapping("/wms/common")
public class BasicCommonController extends BaseController {

    @Resource
    private InventoryManagerUtils inventoryManagerUtils;

    /**
     * 根据物料主键分配仓库
     *
     * @param pk 物料主键
     * @return 仓库集合
     */
    @GetMapping("/warehouse/{pk}")
    public AjaxResult selectNccWarehouse(@PathVariable String pk) {
        return AjaxResult.success(inventoryManagerUtils.selectNccWarehouse(pk, ""));
    }

    /**
     * 验证批次号是否唯一
     *
     * @param batchNo 批号
     * @return 验证结果
     */
    @GetMapping("/batchNo/{batchNo}")
    public AjaxResult checkBatchNo(@PathVariable String batchNo) {
        return inventoryManagerUtils.checkBatchNoUnique(batchNo);
    }

    /**
     * 修改货位
     *
     * @param vbillcode 单据号（CR:采购入；TR：其他入；PR：成品入；XC:销售出；CC：材料出；TC：其他出；ZK:转库）
     * @param freight   货位
     * @return 修改结果
     */
    @PostMapping("/changeFreight/{vbillcode}/{cgeneralbid}/{freight}")
    @PreAuthorize("@ss.hasAnyPermi('order:purchase:goodsLit,order:finished:goodsLit,order:otherinbound:goodsLit," +
            "order:saleout:goodsLit,order:material:goodsLit,order:otherout:goodsLit,order:transfer:goodsLit')")
//    @LogE(title = "修改货位", businessType = BusinessType.UPDATE)
    public AjaxResult changeFreight(@PathVariable("vbillcode") String vbillcode,
                                    @PathVariable("freight") String freight,
                                    @PathVariable("cgeneralbid") String cgeneralbid) {
        return inventoryManagerUtils.changeFreight(vbillcode, freight, cgeneralbid);
    }

    /**
     * 检查货位是否可以用
     *
     * @param warehouse 仓库PK
     * @param freight   货位
     * @return 检查结果
     */
    @GetMapping("/checkFreight")
    public AjaxResult checkFreight(@RequestParam("warehouse") String warehouse,
                                   @RequestParam("freight") String freight) {
        return inventoryManagerUtils.checkFreight(warehouse, freight);
    }

    /**
     * 修改签字日期
     * @param targetMap 修改目标
     * @return 修改结果
     */
    @PostMapping("/changeSignTime")
    @PreAuthorize("@ss.hasRole('admin')")
    public AjaxResult changeSignTime(@RequestBody Map<String, Object> targetMap){
        String username = getUsername();
        targetMap.put("updateBy", username);
        return inventoryManagerUtils.changeSignTime(targetMap);
    }
}
